package com.handinfo.android.game.particle;

import com.handinfo.android.DWGameManager;
import com.handinfo.android.core.graphics.DWGraphics;
import com.handinfo.android.core.object.Actor;
import com.handinfo.android.core.object.Animation;
import com.handinfo.android.ui.UIWindows;
import com.handinfo.android.utils.Tools;

import android.graphics.Bitmap;

public class Particle {

	// 变量
	public float m_x;
	public float m_y;
	public float m_speedx;// X轴速度
	public float m_speedy;// Y轴速度
	public Bitmap[] m_imgAnim;// 粒子动画
	public Actor m_actor;
	public int m_loop;// 粒子动画帧
	public int m_type;// 粒子类型
	public int m_Trajectory;// 轨迹类型
	public long m_curtime;// 时间变量
	public int m_loopWidth = 160;
	public boolean isLife;

	public Particle(int type, int Trajectory) {
		m_type = type;
		m_Trajectory = Trajectory;
		switch (type) {
		case ParticleConstants.类型_元宝:
			m_imgAnim = ParticleConstants.m_gold;
			break;
		case ParticleConstants.类型_花瓣:
			m_imgAnim = ParticleConstants.m_petal;
			break;
		case ParticleConstants.类型_喜灯:
			m_actor = new Actor(ParticleConstants.m_hilight);
			break;
		case ParticleConstants.类型_亲亲:
			m_actor = new Actor(ParticleConstants.m_kiss);
			break;
		case ParticleConstants.类型_闪心:
			m_actor = new Actor(ParticleConstants.m_heart);
			break;
		case ParticleConstants.类型_礼花0:
			int index = Tools.random(0, 3);
			m_actor = new Actor(ParticleConstants.m_effect0[index]);;
			break;
		}
		refresh(false);
	}

	/** 清理 */
	public void dispose() {
		m_imgAnim = null;
	}

	/** 设置轨迹 */
	public void setAttribute(byte type) {
		m_Trajectory = type;
	}

	/** 刷新 */
	public void refresh(boolean islife) {
		if (islife) {
			m_y = 0;
		} else {
			m_y = Tools.random(0, DWGameManager.Screen_Height);
		}
		switch (m_Trajectory) {
		case ParticleConstants.轨迹_左斜线:
			m_x = Tools.random(0, DWGameManager.Screen_Width
					+ (DWGameManager.Screen_Width >> 1));
			int speed = Tools.random(3, 10);
			m_speedx = speed;
			m_speedy = speed;
			break;
		case ParticleConstants.轨迹_右斜线:
			m_x = Tools.random(-(DWGameManager.Screen_Width >> 1),
					DWGameManager.Screen_Width);
			speed = Tools.random(3, 10);
			m_speedx = speed;
			m_speedy = speed;
			break;
		case ParticleConstants.轨迹_花瓣:
			m_x = Tools.random(0, DWGameManager.Screen_Width);
			speed = Tools.random(0, 4);
			m_speedx = Tools.random(0, 1) == 0 ? speed : -speed;
			m_speedy = Tools.random(1, 30);
			m_curtime = System.currentTimeMillis();
			break;
		case ParticleConstants.轨迹_喜灯左:
			m_x = -(m_loopWidth>>1);
			m_y = 0;
			m_speedx = 4;
			m_curtime = -1;
			m_actor.activate();
			m_actor.m_posX = (int) m_x;
			m_actor.m_posY = (int) m_y;
			m_actor.setAction(0, true, true);
			break;
		case ParticleConstants.轨迹_喜灯右:
			m_x = DWGameManager.Screen_Width + (m_loopWidth>>1);
			m_y = 0;
			m_speedx = 4;
			m_curtime = -1;
			m_actor.activate();
			m_actor.m_posX = (int) m_x;
			m_actor.m_posY = (int) m_y;
			m_actor.setAction(0, true, true);
			break;
		case ParticleConstants.轨迹_亲亲静止:
			m_x = DWGameManager.Screen_Width >> 1;
			m_y = DWGameManager.Screen_Height;
			m_curtime = System.currentTimeMillis();
			m_actor.activate();
			m_actor.m_posX = (int) m_x;
			m_actor.m_posY = (int) m_y;
			m_actor.setAction(0, true, true);
			break;
		case ParticleConstants.轨迹_闪心:
			m_x = Tools.random(DWGameManager.Screen_Width / 8,
					DWGameManager.Screen_Width * 7 / 8);
			m_y = Tools.random(DWGameManager.Screen_Height * 3 / 4,
					DWGameManager.Screen_Height);
			m_curtime = Tools.random(0, 10) * 5;
			m_actor.m_isOnce = true;
			m_actor.m_posX = (int) m_x;
			m_actor.m_posY = (int) m_y;
			m_actor.setAction(0, false, true);
			break;
		case ParticleConstants.轨迹_定点礼花0:
			m_x = Tools.random(DWGameManager.Screen_Width / 6,
					DWGameManager.Screen_Width * 5 / 6);
			m_y = Tools.random(DWGameManager.Screen_Height * 1 / 4,
					DWGameManager.Screen_Height * 3 / 5);
			m_curtime = Tools.random(0, 10) * 2;
			m_actor.m_isOnce = true;
			m_actor.m_posX = (int) m_x;
			m_actor.m_posY = (int) m_y;
			m_actor.setAction(0, false, true);
			break;	
		}
	}

	public void paint(DWGraphics g) {
		if (m_imgAnim != null) {
			g.setClip(0, 0, DWGameManager.Screen_Width,
					DWGameManager.Screen_Height);
			g.drawBitmap(m_imgAnim[m_loop], m_x, m_y, DWGraphics.HCENTER
					| DWGraphics.VCENTER);
		}
		if (m_actor != null && m_actor.m_enabled) {
			m_actor.draw(g, 0, 0);
		}
	}

	public void logic() {
		if (m_imgAnim != null) {
			if (m_loop < m_imgAnim.length - 1) {
				m_loop++;
			} else {
				m_loop = 0;
			}
		}
		switch (m_Trajectory) {
		case ParticleConstants.轨迹_左斜线:
			if (m_x <= -50 || m_y > DWGameManager.Screen_Height + 50) {
				refresh(true);
			} else {
				m_x -= m_speedx;
				m_y += m_speedy;
			}
			break;
		case ParticleConstants.轨迹_右斜线:
			if (m_x >= DWGameManager.Screen_Width + 50
					|| m_y > DWGameManager.Screen_Height + 50) {
				refresh(true);
			} else {
				m_x += m_speedx;
				m_y += m_speedy;
			}
			break;
		case ParticleConstants.轨迹_花瓣:
			if (m_y > DWGameManager.Screen_Height + 50) {
				refresh(true);
			} else {
				m_x += m_speedx;
				m_y += m_speedy;
				if (System.currentTimeMillis() - m_curtime > 2000) {
					m_speedx = -m_speedx;
					m_curtime = System.currentTimeMillis();
				}
			}
			break;
		case ParticleConstants.轨迹_喜灯左:
			if (m_actor != null && m_actor.m_enabled) {
				if (m_actor.m_posX <  (m_loopWidth >>1)) {
					 m_actor.m_posX += m_speedx;
				}
				m_actor.nextFrame();
			}
			break;
		case ParticleConstants.轨迹_喜灯右:
			if (m_actor != null && m_actor.m_enabled) {
				if (m_actor.m_posX > (DWGameManager.Screen_Width - (m_loopWidth >>1 ))) {
				     m_actor.m_posX -= m_speedx;
				}
				m_actor.nextFrame();
			}
			break;
		case ParticleConstants.轨迹_亲亲静止:
			if (m_actor != null && m_actor.m_enabled) {
				m_actor.nextFrame();
			}
			break;
		case ParticleConstants.轨迹_闪心:
			if (m_actor != null) {
				if (m_curtime > 0) {
					m_curtime--;
					return;
				} else if (m_curtime == 0) {
					m_actor.activate();
					m_curtime--;
				}
				if (m_actor.m_enabled) {
					m_actor.nextFrame();
				} else {
					refresh(false);
				}
			}
			break;
		case ParticleConstants.轨迹_定点礼花0:
			if (m_actor != null) {
				if (m_curtime > 0) {
					m_curtime--;
					return;
				} else if (m_curtime == 0) {
					m_actor.activate();
					m_curtime--;
				}
				if (m_actor.m_enabled) {
					m_actor.nextFrame();
				} else {
					isLife = true;
				}
			}
			break;
		default:
			break;
		}
	}

}
